from bottle import redirect,request
from functools import wraps
from lib import session

class login:
	@staticmethod
	def login_required(func):
		@wraps(func)
		def decorated_view(*args, **kwargs):
			if 'custom_user' not in getsession() or 'remote_addr' not in getsession():
				return redirect('/login_us')
			else:
				if getsession()['remote_addr']!=request.remote_addr:
					return redirect('/login_us')
			return func(*args, **kwargs)
		return decorated_view

	@staticmethod
	def login_user(func):
		@wraps(func)
		def decorated_view(*args, **kwargs):
			return func(*args, **kwargs)
		return decorated_view

	@staticmethod
	def logout_user(func):
		@wraps(func)
		def decorated_view(*args, **kwargs):
			getsession().pop('custom_user',None)
			getsession().pop('remote_addr',None)
			# getsession().pop('custom_passwd',None)
			return redirect('/login_us')
		return decorated_view

	@staticmethod
	def login(name,passwd):
		getsession()['custom_user']=name
		getsession()['remote_addr']=request.remote_addr
		# getsession()['custom_passwd']=passwd
		return '1'